<?php


namespace app\service\utils;


use app\common\RedisKey;
use app\model\config\RegionModel;
use app\service\BaseService;
use think\facade\Cache;

class RegionService extends BaseService
{

    public function __construct()
    {
        $this->model = new RegionModel();
    }

    /**
     * 获取省ID
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getProvinceListService()
    {
        $key = RedisKey::PROVINCE_LIST;
        $list = Cache::get($key);
        if (!$list) {
            $list = $this->model->where('level', 1)->field('id,name')->select()->toArray();
            Cache::set($key, json_encode($list));
            return $list;
        }
        return json_decode($list);
    }

    /**
     * 根据省ID获取市
     * @param $provinceId
     * @param $name
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCityListByProvinceIdService($provinceId, $name)
    {
        $where = [];
        $where[] = ['pid', '=', $provinceId];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $list = $this->model->where($where)->field('id,name')->select()->toArray();
        return $list;
    }

    /**
     * 根据市ID获取区县
     * @param $cityId
     * @param $name
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAreaListByCityIdService($cityId, $name)
    {
        $where = [];
        $where[] = ['pid', '=', $cityId];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $list = $this->model->where($where)->field('id,name')->select()->toArray();
        return $list;
    }

    /**
     * 获取省市区地址
     * @param $provinceId
     * @param $cityId
     * @param $areaId
     * @return string
     */
    public function getAddress(int $provinceId, int $cityId, int $areaId): string
    {
        $or_str = "FIELD(id,$provinceId,$cityId,$areaId)";
        $list = $this->model->whereIn('id', [$provinceId, $cityId, $areaId])->orderRaw($or_str)->column('name');
        return implode('', $list);
    }

    /**
     * 获取区域名称
     * @param $id
     * @return mixed
     */
    public function getRegionName($id)
    {
        $name = $this->model->where('id', $id)->value('name');
        return $name;
    }

    /**
     * 根据名称查询ID
     * @param $name
     * @return mixed
     */
    public function getRegionIdByName($name)
    {
        $id = $this->model->where('name', 'like', '%' . $name . '%')->value('id');
        return $id;
    }

    /**
     * 根据code获取市ID
     * @param $code
     * @return mixed
     */
    public function getCityIdByCode($code)
    {
        return $this->model->where('code', $code)->where('level', 2)->value('id');
    }

    /**
     * 根据code获取名称
     * @param $code
     * @return mixed
     */
    public function getNameByCode($code)
    {
        return $this->model->where('code', $code)->value('name');
    }

    /**
     * 获取上级ID
     * @param $id
     * @return mixed
     */
    public function getPid($id)
    {
        return $this->model->where('id', $id)->value('pid');
    }
}